angular.module('mamApp').controller('personalInformationController', ['$scope', 'PersonalInformationManager', '$state', '$uibModal', 'FileUploader', 'InitManger', 'api', '$filter', 'BaseValidations','ImsErr',
    function ($scope, PersonalInformationManager, $state, $uibModal, FileUploader, InitManger, api, $filter, BaseValidations,ImsErr) {
        $scope.BaseValidations = BaseValidations;
        $scope.image = false;
        $scope.imageError = false;
        $scope.imageError_a = false;
        $scope.imageError_b = false;
        $scope.image_a = false;
        $scope.image_b = false;
        $scope.pDisabled = false;
        $scope.cDisabled = false;
        $scope.cpTypes = [
            {key: 51, value: $scope.table[51]},
            {key: 52, value: $scope.table[52]},
            {key: 53, value: $scope.table[53]},
            {key: 54, value: $scope.table[54]},
        ];
        //密码修改
        $scope.modify = function () {
            var modalInstance = $uibModal.open({
                animation: true,
                size: 'normal',
                templateUrl: 'reset-password.html',
                controller: 'modifyPasswordController'
            })

        }
        $scope.nature = 13;
        $scope.formData = {};
        var cpId = $scope.userInfo.cpId;
        $scope.step = 1;
        $scope.lastOne = function (nature) {
            $scope.nature = nature;
            $scope.step = 2;

        };
        $scope.upOne = function () {
            $scope.step = 1;
        };
        $scope.upTwo = function () {
            $scope.step = 2;


        };
        $scope.lastTwo = function (nature) {
            $scope.nature = nature;
            $scope.step = 3;
        }

        $scope.formData = {};
        $scope.loadPic = false;


        if ($scope.userInfo.cpProperty == 12) {

        }

        $scope.myImage = '';
        $scope.myCroppedImage = '';

        var handleFileSelect = function (evt) {
            var file = evt.currentTarget.files[0];
            if(file.type.indexOf("image")==-1){
                $scope.$apply($scope.errorImg='仅支持PNG/JPG/JPEG格式');
                return false;
            }else {
                $scope.$apply($scope.errorImg='');
            }

            $scope.uploadCover(file)
        };

        $scope.uploadCover = function (image) {
            if(image.type.indexOf("image")==-1){
                $scope.$apply($scope.errorImg='仅支持PNG/JPG/JPEG格式');
                return false;
            }else {
                $scope.$apply($scope.errorImg='');
            }
            var modalInstance = $uibModal.open({
                animation: true,
                backdrop:false,
                templateUrl: 'uploadCover.html',
                controller: 'coverManageController',
                resolve: {
                    params: function () {
                        return {image: image,coverType:"avatar"};
                    }
                }
            });
            modalInstance.result.then(function (result) {
                $("#fileInput").prop("value","");

                if (result.url) {
                    $scope.formData.avatarUrl = result.url;

                }

            })
        }




        $scope.businessLicenseLoading = false;
        $scope.idCardFrontLoading = false;
        $scope.idCardBackLoading = false;

        setTimeout(function () {
            angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect);
            angular.element(document.querySelector('#businessLicenseSelect')).on('change', function () {
                $scope.businessLicenseLoading = true;
            });
            angular.element(document.querySelector('#idCardFrontSelect')).on('change', function () {
                $scope.idCardFrontLoading = true;
            });
            angular.element(document.querySelector('#idCardBackSelect')).on('change', function () {
                $scope.idCardBackLoading = true;
            });

        }, 1000)
        //地区选择]
        //省/直辖市选择

        InitManger.provinces().then(function (result) {
            $scope.provinces = result;

        }).then(function () {
            if ($scope.userInfo.reviewStatus == 9 && $scope.userInfo.cpProperty == 12) {
                PersonalInformationManager.userInfoDetail().then(function (result) {
                    $scope.citys = InitManger.getCitysByProId(result.data.personalVO.provinces);
                    $scope.userInfo.avatarUrl = result.data.personalVO.avatarUrl;
                    angular.extend($scope.formData, result.data.personalVO);
                    $scope.nature = $scope.userInfo.cpProperty
                    $scope.provinceSelected = $filter('filter')($scope.provinces, {provinceID: result.data.personalVO.provinces})[0];
                    $scope.citySelected = $filter('filter')($scope.citys, {cityID: result.data.personalVO.city})[0];
                });
                $scope.submitInfo = function () {
                    PersonalInformationManager.userInfoEdit($scope.formData).then(function (result) {
                        if (result.data.c == "s") {
                            $scope.userInfo.cpProperty = 12;
                            $scope.userInfo.reviewStatus = 7;
                            localStorage.setItem("userInfo", JSON.stringify($scope.userInfo))
                            $state.reload()

                        } else {
                            alert("系统错误，请重新注册");
                        }
                    })
                };

            }else if( $scope.userInfo.cpProperty == 12){
                PersonalInformationManager.userInfoDetail().then(function (result) {
                    $scope.citys = InitManger.getCitysByProId(result.data.personalVO.provinces);
                    $scope.provinceSelected = $filter('filter')($scope.provinces, {provinceID: result.data.personalVO.provinces})[0];
                    $scope.citySelected = $filter('filter')($scope.citys, {cityID: result.data.personalVO.city})[0];

                });

            }

            else if ( $scope.userInfo.cpProperty == 13) {
                PersonalInformationManager.companyDetail(cpId).then(function (result) {
                    $scope.citys = InitManger.getCitysByProId(result.data.companyVO.provinces);
                    $scope.userInfo.avatarUrl = result.data.companyVO.avatarUrl;
                    angular.extend($scope.formData, result.data.companyVO);
                    $scope.nature = $scope.userInfo.cpProperty
                    $scope.provinceSelected = $filter('filter')($scope.provinces, {provinceID: result.data.companyVO.provinces})[0];
                    $scope.citySelected = $filter('filter')($scope.citys, {cityID: result.data.companyVO.city})[0];
                });
                $scope.submit = function () {
                    delete  $scope.formData.id;
                    PersonalInformationManager.companyInfoEdit(cpId, $scope.formData).then(function (result) {
                        if (result.data.c == "s") {
                            $scope.userInfo.cpProperty = 13;
                            $scope.userInfo.reviewStatus = 7;
                            localStorage.setItem("userInfo", JSON.stringify($scope.userInfo))
                            $state.reload()
                        } else {

                        }
                    })
                }

            }
        })
        //市/选择
        $scope.provinceChange = function (province) {
            $scope.citySelected = {};
            $scope.citys = InitManger.getCitysByProId(province.provinceID);
            $scope.formData.provinces = province.provinceID;

        }
        $scope.cityChanged = function (citySelected) {
            $scope.formData.city = citySelected.cityID;
        }

        //身份证上传

        //正面上传
        $scope.positiveFile = new FileUploader(
            {
                alias: "uploadFile",
                autoUpload: true,
                queueLimit: 1,//文件个数
                removeAfterUpload: true,//上传后删除文件 */
                filters: [{
                    name: 'uploadFile',//后台接收字段
                    fn: function (item, options) {
                        if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(item.name)) {
                            $scope.imageMsg = "最多只能上传不超过5MB的jpg或者png或者JPEG图片";
                            $scope.image_a = true;
                            $scope.imageError_a = true;
                            return false;
                        } else if (item.size > 5 * 1024 * 1024) {
                            $scope.imageMsg = "图片大小5M以下";
                            $scope.image_a = true;
                            $scope.imageError_a = true;

                            return false;
                        } else {
                            $scope.image_a = false;
                            $scope.imageError_a = true;

                            return true;
                        }


                    }
                }],
                onSuccessItem: function (fileItem, response, status, headers) {
                    if(response.c == "s"){
                        $scope.formData.idCardFrontUrl = response.fileVO.webUrl;
                        $scope.idCardFrontLoading = false;
                    }else {
                        ImsErr.open({message:"图片上传失败！"}).then(function (a) {
                        })
                    }


                },
                onErrorItem: function (fileItem, response, status, headers) {
                },
                url: api + "/cp/personal/cover/idCard",
                headers: {'X-Auth-Token': $scope.userInfo.token}
            }
        );
        //反面上传
        $scope.oppositeFile = new FileUploader(
            {
                alias: "uploadFile",
                autoUpload: true,
                queueLimit: 1,//文件个数
                removeAfterUpload: true,//上传后删除文件 */
                filters: [{
                    name: 'uploadFile',//后台接收字段
                    fn: function (item, options) {
                        if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(item.name)) {
                            $scope.imageMsg = "最多只能上传不超过5MB的jpg或者png或者JPEG图片";
                            $scope.image_a = true;
                            $scope.imageError_b = true;

                            return false;
                        } else if (item.size > 5 * 1024 * 1024) {
                            $scope.imageMsg = "图片大小5M以下";
                            $scope.image_a = true;
                            $scope.imageError_b = true;
                            return false;
                        } else {
                            $scope.image_a = false;
                            $scope.imageError_b = true;

                            return true;
                        }


                    }
                }],
                onSuccessItem: function (fileItem, response, status, headers) {
                    if(response.c == "s"){
                        $scope.formData.idCardBackUrl = response.fileVO.webUrl;
                        $scope.idCardBackLoading = false;
                    }else {
                        ImsErr.open({message:"图片上传失败！"}).then(function (a) {
                        })
                    }

                },
                onErrorItem: function (fileItem, response, status, headers) {
                },
                url: api + "/cp/personal/cover/idCard",
                headers: {'X-Auth-Token': $scope.userInfo.token}
            }
        );

        //营业执照上传
        $scope.businessLicenseUrlFile = new FileUploader(
            {
                alias: "uploadFile",
                autoUpload: true,
                queueLimit: 1,//文件个数
                removeAfterUpload: true,//上传后删除文件 */
                filters: [{
                    name: 'uploadFile',//后台接收字段
                    fn: function (item, options) {
                        if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(item.name)) {
                            $scope.imageMsg = "最多只能上传不超过5MB的jpg或者png或者JPEG图片";
                            $scope.image = true;
                            $scope.imageError = true;
                            return false;
                        } else if (item.size > 5 * 1024 * 1024) {
                            $scope.imageMsg = "图片大小5M以下";
                            $scope.image = true;
                            $scope.imageError = true;
                            return false;
                        } else {
                            $scope.image = false;
                            $scope.imageError = true;
                            return true;
                        }


                    }
                }],
                onSuccessItem: function (fileItem, response, status, headers) {
                    if(response.c == "s"){
                        $scope.formData.businessLicenseUrl = response.fileVO.webUrl;
                        $scope.businessLicenseLoading = false;
                    }else {
                        ImsErr.open({message:"图片上传失败！"}).then(function (a) {
                        })
                    }

                },
                onErrorItem: function (fileItem, response, status, headers) {
                },
                url: api + "/cp/company/companies/cover/license",
                headers: {'X-Auth-Token': $scope.userInfo.token}
            }
        );
        $scope.submitInfo = function () {
            $scope.pDisabled = true;
            PersonalInformationManager.userInfoAdd($scope.formData).then(function (result) {
                $scope.pDisabled = false;
                if (result.data.c == "s") {
                    $scope.userInfo.cpProperty = 12;

                    localStorage.setItem("userInfo", JSON.stringify($scope.userInfo))
                    $state.reload()

                } else {
                    alert("系统错误,重新输入");
                }
            })
        };
        $scope.submit = function () {
            $scope.cDisabled = true;
            PersonalInformationManager.companyInfoAdd($scope.formData).then(function (result) {
                $scope.cDisabled = false;
                if (result.data.c == "s") {
                    $scope.userInfo.cpProperty = 13;
                    localStorage.setItem("userInfo", JSON.stringify($scope.userInfo))
                    $state.reload()
                } else {

                }
            })
        }

        if ($scope.userInfo.cpProperty == 12) {
            PersonalInformationManager.userInfoDetail().then(function (result) {
                $scope.userInfo.avatarUrl = result.data.personalVO.avatarUrl;
                $scope.data = result.data.personalVO;
            });
        }

        if ($scope.userInfo.cpProperty == 13) {
            PersonalInformationManager.companyDetail(cpId).then(function (result) {
                $scope.userInfo.avatarUrl = result.data.companyVO.avatarUrl;
                $scope.data = result.data.companyVO;
            });
        }
        $scope.agree=false;
        $scope.agreement=false;

        $scope.ment=function(){
            $scope.agree=true;
        }
        $scope.protocol=function(){
            $scope.agree=false;
            $scope.agreement=true;
        }

    }])

